﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace ProjectTask
{
    public partial class FrmTaskList : Form
    {
        ProjectCommon.ClassCommon cc;
        DataTable dtTaskList;

        public FrmTaskList(ProjectCommon.ClassCommon aClassCommon)
        {
            InitializeComponent();
            cc = aClassCommon;
        }

        private void GetTaskTable(string aWhere)
        {
            string sql = @"select * 
                            from View_JobTask 
                            where "+aWhere+@" 
                            order by 任务编号 desc";
            SqlDataAdapter sda = new SqlDataAdapter(sql, cc.scKengee_IT_MIS);
            dtTaskList = new DataTable();
            cc.scKengee_IT_MIS.Open();
            sda.Fill(dtTaskList);
            cc.scKengee_IT_MIS.Close();
            sda.Dispose();            
            dtTaskList.Columns["创建时间"].DataType=System.Type.GetType("System.String");
            dgvTask.Columns.Clear();
            dgvTask.DataSource = dtTaskList;

            DataGridViewCheckBoxColumn dgvcbc = new DataGridViewCheckBoxColumn();
            dgvcbc.Name = "选择";
            dgvcbc.HeaderText = "选择";
            dgvTask.Columns.Add(dgvcbc);
            dgvcbc.ReadOnly = true;
            dgvcbc.DisplayIndex = 0;
            dgvTask.AutoSize = true;
            dgvTask.Columns["选择"].Frozen = true;
            dgvTask.Columns["门店名称"].Frozen = true;
        }


        private int SetTaskEngineer(string sJobTask, string sPlanEngineer)
        {
            int result = 0;                       
            string sql = @"UPDATE [kengee_it_mis].[dbo].[JobTask]
                               SET [tbPlanEngineer] = '" + sPlanEngineer + @"'
                             WHERE ID='" + sJobTask + "'";
            sql = ProjectCommon.ClassCommon.ToDBC(sql);
            SqlCommand scm = new SqlCommand(sql, cc.scKengee_IT_MIS);
            cc.scKengee_IT_MIS.Open();
            result = scm.ExecuteNonQuery();
            cc.scKengee_IT_MIS.Close();
            scm.Dispose();
            return result;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            GetTaskTable("(len(任务状态)<1) or (任务状态 not like '%已%')");
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (dgvTask.CurrentRow != null)
            {
                string sAutoID = dgvTask.CurrentRow.Cells["任务编号"].Value.ToString();
                FrmTaskInfo ft = new FrmTaskInfo(cc, ProjectCommon.ClassCommon.OperateState.osModify, sAutoID);
                this.Hide();
                if(ft.ShowDialog()==DialogResult.OK)
                {
                    GetTaskTable("(len(任务状态)<1) or (任务状态 not like '%已%')");  
                }
                this.Show();
                ft.Dispose();
            }
        }

        private void btnAppend_Click(object sender, EventArgs e)
        {
            FrmTaskInfo ft = new FrmTaskInfo(cc, ProjectCommon.ClassCommon.OperateState.osNew, string.Empty);
            this.Hide();

            if (ft.ShowDialog() == DialogResult.OK)
            {
                GetTaskTable("(len(任务状态)<1) or (任务状态 not like '%已%')");    
            }
            this.Show();
            
            ft.Dispose();
        }

        private void btnFind_Click(object sender, EventArgs e)
        {
            if (dgvTask.CurrentRow != null)
            {
                string ColumnName = dgvTask.Columns[dgvTask.CurrentCell.ColumnIndex].Name;
                if (ColumnName.Equals("选择")==false)
                {
                    string sFilter = dgvTask.Columns[dgvTask.CurrentCell.ColumnIndex].Name + " like '%" + tbFindKey.Text + "%'";
                    DataTable dtSelect = ProjectCommon.ClassCommon.GetSelectTable(dtTaskList, sFilter);
                    dgvTask.DataSource = dtSelect;
                }
            }
        }

        private void FrmTaskList_Shown(object sender, EventArgs e)
        {
            string sPowerRight = cc.dtLoginUser.Rows[0]["PowerRight"].ToString();
            btnModify.Enabled = ProjectCommon.ClassCommon.SetPowerRight(sPowerRight[2]);
            btnTaskPlan.Enabled = ProjectCommon.ClassCommon.SetPowerRight(sPowerRight[3]);
            btnPending.PerformClick();
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            if (dgvTask.CurrentRow != null)
            {
                string sFastCode = dgvTask.CurrentRow.Cells["门店快速代码"].Value.ToString();
                ProjectShop.FrmShopInfo fsi = new ProjectShop.FrmShopInfo(cc, ProjectCommon.ClassCommon.OperateState.osView, sFastCode);
                fsi.ShowDialog();
                fsi.Dispose();
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            GetTaskTable("1=1");
        }

        private void tbFindKey_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                btnFind.PerformClick();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (dgvTask.CurrentRow != null)
            {
                string sTaskState = dgvTask.CurrentRow.Cells["任务状态"].Value.ToString();
                if(sTaskState.Equals("已完成"))
                {
                    MessageBox.Show("该任务“已完成”，无需要处理");
                }
                else if (sTaskState.Equals("已放弃"))
                {
                    MessageBox.Show("该任务“已放弃”，无需要处理");
                }
                else
                {
                    string sID = dgvTask.CurrentRow.Cells["任务编号"].Value.ToString();
                    FrmTaskDeal ftd = new FrmTaskDeal(cc, ProjectCommon.ClassCommon.OperateState.osNew, sID);
                    if (ftd.ShowDialog() == DialogResult.OK)
                    {
                        GetTaskTable("(len(任务状态)<1) or (任务状态 not like '%已%')"); 
                    }
                    ftd.Dispose();
                }
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (dgvTask.CurrentRow != null)
            {
                string sID = dgvTask.CurrentRow.Cells["任务编号"].Value.ToString();
                FrmTaskDealList ftdl = new FrmTaskDealList(cc, sID);
                ftdl.ShowDialog();
                ftdl.Dispose();
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            ProjectCommon.FrmInputBox fib = new ProjectCommon.FrmInputBox(string.Empty, "请输入计划执行人的姓名:");
            if (fib.ShowDialog() == DialogResult.OK)
            {
                int SelectCount = Convert.ToInt32(tsslSelectCount.Text);
                if (SelectCount < 1)
                {
                    if (dgvTask.CurrentRow != null)
                    {
                        string sTaskID = dgvTask.CurrentRow.Cells["任务编号"].Value.ToString();
                        SetTaskEngineer(sTaskID, fib.tbInput.Text);
                    }
                }
                else
                {
                    for (int i = 0; i < dgvTask.RowCount; i++)
                    {
                        if (Convert.ToBoolean(dgvTask.Rows[i].Cells["选择"].EditedFormattedValue))
                        {
                            string sTaskID = dgvTask.Rows[i].Cells["任务编号"].Value.ToString();
                            SetTaskEngineer(sTaskID, fib.tbInput.Text);
                        }
                    }
                }
                btnPending.PerformClick();
            }
            fib.Dispose();
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            for (int i = 0; i < dgvTask.RowCount; i++)
            {
                dgvTask.Rows[i].Cells["选择"].Value = (sender as CheckBox).Checked;
            }
        }

        private void 处理任务ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            btnTaskDeal.PerformClick();
        }

        private void 分派任务ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            btnTaskPlan.PerformClick();
        }

        private void 查看处理信息ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            btnDealInfo.PerformClick();
        }

        private void 查看门店信息ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            btnShopInfo.PerformClick();
        }

        private void button1_Click_2(object sender, EventArgs e)
        {
            string MyName = cc.dtLoginUser.Rows[0]["RealName"].ToString();
            string sFilter = "计划执行人 = '" + MyName + "'";
            DataTable dtSelect = ProjectCommon.ClassCommon.GetSelectTable(dtTaskList, sFilter);
            dgvTask.DataSource = dtSelect;
        }

        private void dgvTask_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            if((e.ColumnIndex>0)&&(e.RowIndex>0)&&(e.Button == MouseButtons.Right))
            {
                if (e.RowIndex >= 0)
                {
                    //若行已是选中状态就不再进行设置
                    if (dgvTask.Rows[e.RowIndex].Selected == false)
                    {
                        dgvTask.ClearSelection();
                        dgvTask.Rows[e.RowIndex].Selected = true;
                    }
                    //只选中一行时设置活动单元格
                    if (dgvTask.SelectedRows.Count == 1)
                    {
                        dgvTask.CurrentCell = dgvTask.Rows[e.RowIndex].Cells[e.ColumnIndex];
                    }
                    //弹出操作菜单
                    contextMenuStrip1.Show(MousePosition.X, MousePosition.Y);
                }
            }
        }

        private void dgvTask_DataSourceChanged(object sender, EventArgs e)
        {
            tsslRowCount.Text = dgvTask.RowCount.ToString();
        }

        private int GetdgvChoiceCount()
        {
            int result = 0;
            for (int i = 0; i < dgvTask.RowCount; i++)
            {
                if (Convert.ToBoolean(dgvTask.Rows[i].Cells["选择"].EditedFormattedValue))
                {
                    result += 1;
                }
            }
            return result;
        }

        private void dgvTask_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if ((e.ColumnIndex>0)&&(e.RowIndex>0)&&(dgvTask.Columns[e.ColumnIndex].HeaderText == "选择"))
            {
                bool ChoiceValue = Convert.ToBoolean(dgvTask.Rows[e.RowIndex].Cells[e.ColumnIndex].Value);
                if (ChoiceValue)
                {
                    dgvTask.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = false;
                }
                else
                {
                    dgvTask.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = true;
                }
                //DataGridViewDataErrorContexts dgvdec = new DataGridViewDataErrorContexts();
                //dgvTask.CommitEdit(dgvdec);
                //dgvTask.EndEdit();
                tsslSelectCount.Text = GetdgvChoiceCount().ToString();
            }
        }
    }
}